import argparse
import logging
import multiprocessing
import os


def parse_args():
    parser = argparse.ArgumentParser(description="公共数据库爬虫入口")

    parser.add_argument('-m', '--modules', nargs='+', required=True, help='要运行的模块名（支持多个）')
    parser.add_argument('-i', '--interval_range', type=float, nargs=2, metavar=("MIN", "MAX"), help='请求间隔时间范围（例如 3 10 表示每次间隔在3到10秒之间）')
    parser.add_argument('-t', '--threads', type=int, default=1, help='每个模块的进程数倍数')
    parser.add_argument('-l', '--log-level', type=str, choices=['debug', 'info', 'warning', 'error'], default='info')
    parser.add_argument('-o', '--output', type=str, default='./output', help='结果输出目录')
    parser.add_argument('--retry', type=int, default=3, help='失败重试次数')
    parser.add_argument('--timeout', type=float, default=10.0, help='请求超时时间（秒）')
    parser.add_argument('--dry-run', action='store_true', help='只打印调度计划，不实际运行')

    return parser.parse_args()


def setup_logging(level):
    log_dir = "logs"
    os.makedirs(log_dir, exist_ok=True)
    log_file = os.path.join(log_dir, "omim.log")

    log_level = getattr(logging, level.upper(), logging.INFO)

    for handler in logging.root.handlers[:]:
        logging.root.removeHandler(handler)

    console_handler = logging.StreamHandler()
    console_handler.setLevel(log_level)
    console_formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
    console_handler.setFormatter(console_formatter)

    file_handler = logging.FileHandler(log_file, encoding="utf-8")
    file_handler.setLevel(logging.ERROR)
    file_formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
    file_handler.setFormatter(file_formatter)

    logging.getLogger().setLevel(logging.DEBUG)
    logging.getLogger().addHandler(console_handler)
    logging.getLogger().addHandler(file_handler)


def run_spider(module_name, interval_range, retry, timeout, output_dir, log_level):
    setup_logging(log_level)
    try:
        logging.info(f"模块【{module_name}】启动，间隔范围 {interval_range}s，超时 {timeout}s，输出目录 {output_dir}")
        module = __import__(f"spiders.{module_name}", fromlist=[''])
        module.run(interval_range=interval_range, retry=retry, timeout=timeout, output_dir=output_dir)
    except Exception as e:
        logging.error(f"模块【{module_name}】运行失败: {e}")


def main():
    args = parse_args()
    setup_logging(args.log_level)

    if not os.path.exists(args.output):
        os.makedirs(args.output)

    logging.info(f"将启动模块: {args.modules}")

    if args.dry_run:
        for m in args.modules:
            logging.info(f"计划运行模块【{m}】，进程数: {args.threads}")
        return

    processes = []
    for m in args.modules:
        for i in range(args.threads):
            p = multiprocessing.Process(
                target=run_spider,
                args=(m, args.interval_range, args.retry, args.timeout, args.output, args.log_level)
            )
            processes.append(p)
            p.start()

    for p in processes:
        p.join()


if __name__ == '__main__':
    main()
